import React from 'react';
import { Button, message } from 'antd'
import { crawlerList } from '../../../services/fetch';
export default class LeftCrawler extends React.Component {
  state = {
    queryParams: {
      index: 1,
      page_size: 10
    },
    crawlerData: {
      list: []
    }
  }

  componentDidMount() {
    this.init()
  }

  init = async () => {
    const { queryParams } = this.state
    const { data: { code, data } } = await crawlerList(queryParams)
    if (code === 200) {
      this.setState({ crawlerData: data })
    }
  }

  render() {
    const { crawlerData } = this.state

    return (
      <div>
        {crawlerData.list.length > 0 ?
          <div className="pushData">
            {crawlerData.list.map((item) => {
              return (
                <div className="item" key={item.information_id} >
                  <img className="i-img" src={item.thumbnail} />
                  <div className="push-title">{item.information_title}</div>
                  <div className="bg">
                    <span>{item.create_time}</span>
                  </div>
                  <div className="action">
                    <Button size="small" className="mr10" onClick={() => {
                      let data = {};
                      data = { ...item, type: 1, mType: 'information' }
                      // data.type = 1;
                      // data.information_title = item.information_title;
                      // data.key_words = item.key_words;
                      // data.intro = item.intro;
                      // data.content = item.content
                      // data.information_img = item.information_img;
                      // data.mType = 'information';
                      window.PubSub.publish('setFileFabu', data)
                      localStorage.removeItem('moc_id')
                    }}>使用</Button>
                  </div>
                </div>
              )
            })}
            <div>
              <Button style={{ marginLeft: '10px' }} onClick={() => {
                let { queryParams } = this.state
                if (queryParams.index <= 1) {
                  message.error('已经是第一页');
                  return;
                }
                let page = queryParams.index - 1;
                this.setState({
                  queryParams: { ...queryParams, index: page }
                }, () => { this.init() })

              }}>上一页</Button>
              <Button style={{ marginLeft: '20px' }} onClick={() => {
                let { queryParams, crawlerData } = this.state
                if (queryParams.index >= crawlerData.total_page) {
                  message.error('已经是最后一页');
                  return;
                }
                let page = queryParams.index + 1;
                this.setState({
                  queryParams: { ...queryParams, index: page }
                }, () => { this.init() })
              }}>下一页</Button>
            </div>

          </div>
          : <div className="note"> 暂无数据</div>}
      </div>

    )
  }
}